package tree;


import tree.data.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的所有路径
 *
 * https://leetcode.cn/problems/binary-tree-paths/solutions/400326/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/
 */
public class BinaryTreePaths {



    List<String> paths=new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {

        constructPaths(root,"");

        return paths;
    }


    public void constructPaths(TreeNode node,String path){

        if (node==null){
            return;
        }

        StringBuilder sb=new StringBuilder();
        sb.append(path);
        sb.append(node.val);

        if (node.left==null&&node.right==null){
            paths.add(sb.toString());
        }else{
            sb.append("->");
            constructPaths(node.left,sb.toString());
            constructPaths(node.right,sb.toString());
        }
    }

}
